package com.ybkj.daijia.api.apirest;

import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.config.SalesApi;
import com.ybkj.daijia.server.sales.Activity;
import com.ybkj.daijia.server.sales.ActivityVo;
import com.ybkj.daijia.server.sales.CouponRule;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.CouponRuleService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("ApiActivityWebProcessController")
@RequestMapping(value = "activityweb")
public class ActivityWebProcessController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private CouponRuleService couponRuleService;
    @Autowired
    private SalesApi salesApi;


    @RequestMapping(value = "phonegift/{id}", method = RequestMethod.GET)
    public ModelAndView phoneGift(@PathVariable("id") Long id) {

        ModelAndView mv = new ModelAndView();
        Activity activity = activityService.findOne(id);
        mv.addObject("activity", activity);
        mv.setViewName("activity/phonegift");

        return mv;

    }

    @RequestMapping(value = "success/{id}", method = RequestMethod.GET)
    public ModelAndView success(@PathVariable("id") Long id,
        @RequestParam(required = true, value = "cids") String cids,
        @RequestParam(required = true, value = "cas") String cas) {
        ModelAndView mv = new ModelAndView();
        Activity activity = activityService.findOne(id);

        String[] cidsstr = cids.split("_");
        String[] casstr = cas.split("_");

        List<CouponRule> couponRuleList = new ArrayList<CouponRule>();

        if (cidsstr.length > 0) {
            for (int i = 0; i < cidsstr.length; i++) {
                CouponRule couponRule = couponRuleService.findOne(Long.valueOf(cidsstr[i]));
                Integer amount = Integer.valueOf(casstr[i]);
                for (int j = 0; j < amount; j++) {
                    couponRuleList.add(couponRule);
                }
            }
        }

        mv.addObject("couponRuleList", couponRuleList);
        mv.addObject("activity", activity);
        mv.setViewName("activity/success");

        return mv;


    }


    @RequestMapping(value = "phonecoupongift", method = RequestMethod.POST)
    public @ResponseBody
    Result<ActivityVo> phoneCouponGift(
        @RequestParam(required = true, value = "activityId") Long activityId,
        @RequestParam(required = true, value = "phone") String phone) {

        Result<ActivityVo> result = new Result<ActivityVo>();

        result = activityService.phoneGift(activityId, phone);

        return result;

    }


}
